package com.jufanshare.myblog.service.account;

import com.jufanshare.myblog.common.dto.AjaxRes;
import com.jufanshare.myblog.entity.Account;
import com.jufanshare.myblog.service.base.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;

/**
 * 作用：
 * 作者：Tiddler
 * 时间：2019/1/27 10：58
 * 类名： AccountServiceImpl
 **/
@Service
public class AccountServiceImpl extends BaseServiceImpl<Account> implements AccountService {
    @Transactional
    @Override
    public AjaxRes doLogin(Account account, HttpSession session) {
        AjaxRes res=new AjaxRes();
        Account findEntity=new Account();
        findEntity.setUsername(account.getUsername());
        Account account1 = templateOne(findEntity);
        if(account1==null||account1.getId()==null||account1.getId()==0){
            res.setFailMsg("没有此账号");
            return res;
        }
        if(account1.getTestloginnumber()>=3){
            res.setFailMsg("输错密码3次以上，该账号已被锁死");
            return res;
        }
        String passwordBySalt = "";//此处为了安全，原先代码已删除【密码加盐等操作】
        if(passwordBySalt.equals(account1.getPassword())){
            account1.setTestloginnumber(0);
            updateById(account1);//尝试登录次数清零
            session.setAttribute("account_session_key",account1);
            res.setRes(true);
        }else {
            int loginCount=account1.getTestloginnumber()+1;
            account1.setTestloginnumber(loginCount);
            updateById(account1);//尝试登录次数+1
            res.setFailMsg("密码错误，已输错密码"+loginCount+"次，出错3次将被锁定");
        }
        return res;
    }
}
